/*                                                                         
 * Copyright 2010-2013 the original author or authors.                     
 *                                                                         
 * Licensed under the Apache License, Version 2.0 (the "License");         
 * you may not use this file except in compliance with the License.        
 * You may obtain a copy of the License at                                 
 *                                                                         
 *      http://www.apache.org/licenses/LICENSE-2.0                         
 *                                                                         
 * Unless required by applicable law or agreed to in writing, software     
 * distributed under the License is distributed on an "AS IS" BASIS,       
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and     
 * limitations under the License.                                          
 */
package com.shopng.geode.api;

import java.util.Map;
import java.util.TreeMap;

import com.shopng.geode.common.JsonUtils;

/**
 * 
 * @author sgao20
 *
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public final class JsonObject
{
  private Map<String, Object> values;
  
  public static void main(String[] args)
  {
    JsonObject jo = new JsonObject().append("col1", "val1").append("col2", "val2");
    String joStr = jo.toString();
    
    JsonObject newJo = JsonObject.append(joStr);
    newJo.append("col3", "val3");
    newJo.append("col2", "val2222");
    
    System.out.println(newJo.toString());
    
    
  }
  
  /**
   * 
   */
  private JsonObject()
  {
    values = new TreeMap<>();
  }
  
  
  public final static JsonObject append(String jsonValue)
  {
    JsonObject jo = new JsonObject();
    jo.values = new TreeMap<>();
    Map m = JsonUtils.jsonToObject(jsonValue, Map.class);
    if (m != null)
      jo.values.putAll(m);
    return jo;
  }
  
  public final JsonObject append(JsonObject value)
  {
    if (value != null)
      values.putAll(value.values);
    return this;
  }
  
  public final static JsonObject create()
  {
    JsonObject jo = new JsonObject();
    jo.values = new TreeMap<>();
    return jo;
  }
  
  public JsonObject append(String key, String value)
  {
    values.put(key, value);
    return this;
  }
  
  public JsonObject append(String key, Object value)
  {
    values.put(key, value);
    return this;
  }
  
  public String getValue(String key)
  {
    return String.valueOf(values.get(key));
  }
  
  public <T> T getValue(String key, Class<T> ct)
  {
    return (T) values.get(key);
  }
  
  public String toJson()
  {
    return JsonUtils.objectToJson(values);
  }
  
  public Map<String, Object> toMap()
  {
    return values;
  }
  
  /*
   * (non-Javadoc)
   * 
   * @see java.lang.Object#toString()
   */
  @Override
  public String toString()
  {
    return toJson();
  }
}
